home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 November: Tool Chest / Dev.CD Nov 96 TC / Dev.CD Nov 96 TC.toast / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS 1.31 / Documentation / UPDAT110.txt < prev    next >
Encoding:
Text File  |  1995-03-10  |  59.8 KB  |  2,377 lines  |  [TEXT/MPS ]

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.          The Purdue Compiler Construction Tool Set
  8.                  Version 1.10 Release Notes
  9.  
  10.                        ANTLR and DLG
  11.  
  12.                       August 31, 1993
  13.  
  14.                         Terence Parr
  15.       Army High Performance Computing Research Center,
  16.                       University of MN
  17.                       (parrt@acm.org)
  18.  
  19.                  Will Cohen and Hank Dietz
  20.               School of Electrical Engineering
  21.                      Purdue University
  22.                   (cohenw@ecn.purdue.edu)
  23.                    (hankd@ecn.purdue.edu)
  24.  
  25.  
  26. 1.  Introduction
  27.  
  28.  
  29.      This document  describes  the  changes/enhancements  in
  30. PCCTS  since  version 1.06.  As with the 1.06 release notes,
  31. these notes do not constitute the complete reference manual.
  32. Unfortunately, the reference manual is in the same condition
  33. as it was for the 1.00 release in the Spring  of  1992.   We
  34. are  working  on  a total rewrite of the manual, which might
  35. end up in a book consisting of the theory  behind  practical
  36. k-token  lookahead  for  k>1 (Terence Parr's Ph.D.  thesis),
  37. ANTLR implementation notes, and the PCCTS user's manual.
  38.  
  39.      The 1.10 release of PCCTS has four  main  enhancements:
  40. fully  implemented  semantic predicates (<<...>>?), infinite
  41. lookahead  (plus  selective  backtracking  that  uses   it),
  42. increased  ANTLR (see -ck and ZZUSE_MACROS sections) and DLG
  43. speed, and  the  ability  to  link  multiple  ANTLR  parsers
  44. together.   A  number of bug fixes have been incorporated as
  45. well.  The tutorials have not been  updated  much  for  this
  46. release.
  47.  
  48.      To  better  support  our  user's, we have established a
  49. mailing list called pccts-users that you can subscribe to by
  50. sending email to pccts-users-request@arc.umn.edu with a body
  51. of "subscribe pccts-users your-name".  Users that have  reg-
  52. istered  with  the PCCTS mail server at pccts@ecn.purdue.edu
  53. have not been automatically subscribed.  Once you have  sub-
  54. scribed,  posting  a  message  to  the PCCTS community is as
  55. _________________________
  56. Partial  support  for  this work has come from the Army
  57. Research Office contract number  DAAL03-89-C-0038  with
  58. the  Army High Performance Computing Research Center at
  59. the U of MN.
  60.  
  61.  
  62.  
  63.  
  64.                      September 14, 1994
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                             - 2 -
  71.  
  72.  
  73. simple as sending email to pccts-users@ahpcrc.umn.edu  (with
  74. any Subject: and body).  You can also send a body of help to
  75. pccts-users-request@ahpcrc.umn.edu to get help on using  the
  76. mailing list server.
  77.  
  78.      We  have finally agreed on a numbering scheme for PCCTS
  79. releases: x.yz where x reflects  the  major  release  number
  80. (new  tool  additions),  y  reflects major new feature addi-
  81. tions, and z reflects bug fixes and minor feature  additions
  82. (minor releases).
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.                      September 14, 1994
  131.  
  132.  
  133.  
  134.  
  135.  
  136.                             - 3 -
  137.  
  138.  
  139. 2.  Semantic Predicates
  140.  
  141.      The fundamental idea behind semantic predicates has not
  142. changed since the 1.06 release -- semantic predicates  indi-
  143. cate  the  semantic validity of continuing with the parse or
  144. of predicting a particular production.  However, we now col-
  145. lect  all  predicates  visible  to a syntactically ambiguous
  146. parsing decision rather than just the first one  encountered
  147. as  in  1.06.  In addition, the context of the predicate can
  148. be computed and hoisted with the predicates; helpful warning
  149. are  also  generated  for  incompletely disambiguated parser
  150. decisions.  The only  backward  incompatibilities  are  that
  151. parsing does not halt automatically if a semantic validation
  152. predicate fails and the -pr is obsolete -- the specification
  153. of a predicate implies that it may be used by ANTLR to vali-
  154. date and disambiguate as it sees fit.  In  this  section  we
  155. discuss all of these issues.
  156.  
  157. 2.1.  Visible Semantic Predicates
  158.  
  159.      Given  a  syntactically  ambiguous parser decision (or,
  160. more  accurately,  a  non-deterministic   decision),   ANTLR
  161. attempts  to  resolve  it with semantic information -- ANTLR
  162. searches for visible predicates.  A visible predicate  is  a
  163. semantic predicate that could be evaluated without consuming
  164. an input token or executing a user action  (except  initial-
  165. ization  actions,  which  generally  define variables).  All
  166. visible predicates reside on the left edge  of  productions;
  167. predicates not on the left edge can only function as valida-
  168. tion predicates (see 1.06 release notes).  Consider a simple
  169. example:
  170.  
  171.  
  172. a       :       <<pred1>>?  ID
  173.         |       <<pred2>>?  ID
  174.         ;
  175.  
  176.  
  177. Assuming  that lookahead information is insufficient to pre-
  178. dict productions of rule a, ANTLR  would  incorporate  pred1
  179. into  the prediction expression for the first production and
  180. pred2 into the second prediction expression.  Multiple pred-
  181. icates  can  be  hoisted  (which  may or may not be what you
  182. want):
  183.  
  184.  
  185. decl:   <<pred1>>? var
  186.         |       <<pred2>>? ID
  187.         ;
  188. var     :       <<is_var(LATEXT(1))>>? ID
  189.         ;
  190.  
  191.  
  192. In this case, the prediction expression for  production  one
  193.  
  194.  
  195.  
  196.                      September 14, 1994
  197.  
  198.  
  199.  
  200.  
  201.  
  202.                             - 4 -
  203.  
  204.  
  205. of rule decl would resemble (with context computation turned
  206. off -- see below):
  207.  
  208.  
  209. if ( pred1 && is_var(LATEXT(1)) && LA(1)==ID ) {
  210.         var();
  211. }
  212.  ...
  213.  
  214.  
  215. Here, two visible predicates were found to disambiguate  the
  216. prediction of the first production of rule decl whereas only
  217. one was found for the prediction of the second production.
  218.  
  219.      The action of evaluating a predicate in a  decision  is
  220. called  hoisting.   In  the  first  example of this section,
  221. predicates were not moved since they reside at the  decision
  222. point  in  rule  a,  but  technically  we say that they were
  223. hoisted into the decision.  In the second example, pred1 was
  224. hoisted  from  decl  and  is_var(LATEXT(1)) was hoisted from
  225. rule var to predict the first production of decl.
  226.  
  227. 2.2.  Context of Semantic Predicates
  228.  
  229.      In release 1.06, predicates were hoisted  without  com-
  230. puting  and hoisting the context of that predicate.  Context
  231. is important because, as we saw in the last section,  predi-
  232. cates  may be evaluated in totally different rules.  Imagine
  233. a rule that had many alternative productions, two  of  which
  234. were  syntactically  nondeterministic  because  of  a common
  235. lookahead of ID (assuming that only one symbol of  lookahead
  236. is available for simplicity).
  237.  
  238.  
  239. a       :       A ...
  240.         |       B ...
  241.         |       classname
  242.         |       C ...
  243.         |       varname
  244.         |       D ...
  245.         ;
  246. classname
  247.         :       <<pred1>>? ID
  248.         ;
  249. varname
  250.         :       <<pred2>>? ID
  251.         ;
  252.  
  253.  
  254. Simply  incorporating  predi  into the production prediction
  255. expressions for alternatives three and five is not safe  for
  256. two reasons:
  257.  
  258. [1]  Evaluation of predi may cause a program execution error
  259.  
  260.  
  261.  
  262.                      September 14, 1994
  263.  
  264.  
  265.  
  266.  
  267.  
  268.                             - 5 -
  269.  
  270.  
  271.      if evaluated on the wrong type of data.  predi will  be
  272.      evaluated  on  any  input, which is {A, B, C, D, ID} in
  273.      our case -- predi may "core" if fed non-ID token types.
  274.  
  275. [2]  predi  may  give misleading results even if it does not
  276.      "core".  predi may return false even though the produc-
  277.      tion  is  not dependent on the predicate for that token
  278.      type.  For example:
  279.  
  280.  
  281.      a   :   (var | NUM) ...
  282.          |   <<!is_var(LATEXT(1))>>?  ID ...
  283.          ;
  284.      var :   <<is_var(LATEXT(1))>>?   ID
  285.          ;
  286.  
  287.  
  288.      The first production will never  match  a  NUM  because
  289.      is_var(LATEXT(1))  will  always  evaluate  to false for
  290.      that token type since numbers are  not  variables  ever
  291.      (the  predicate  in var is hoisted for use in the deci-
  292.      sion for rule a).
  293.  
  294. The way to solve both problems is to change predi to:
  295.  
  296.  
  297. LA(1)==ID ? predi : 1
  298.  
  299.  
  300. The 1 merely indicates that if the lookahead is  not  an  ID
  301. then  enable the production for normal parsing -- we have no
  302. semantic information that establishes the validity or  inva-
  303. lidity of the production.
  304.  
  305.      Context  computation similar to this is can now be done
  306. automatically (-prc on).  Unfortunately, as mentioned previ-
  307. ously  in  this document, computing full LL(k) lookahead is,
  308. in general, an exponential problem; hence, for  large  gram-
  309. mars  you  may want to keep this off with -prc off (default)
  310. and include context tests manually in your predicates.   The
  311. old  -pr  option  to  turn on parsing with predicates is now
  312. ignored as the specification of a predicate  indicates  that
  313. it should be used.
  314.  
  315.      ANTLR  does  its  best to warn the user when a possibly
  316. incompletely disambiguated grammar has been  specified.   In
  317. other  words,  when  a  syntactically  ambiguous decision is
  318. resolved with semantic predicates,  all  mutually  ambiguous
  319. productions  must have at least one semantic predicate asso-
  320. ciated with it.  For example:
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.                      September 14, 1994
  329.  
  330.  
  331.  
  332.  
  333.  
  334.                             - 6 -
  335.  
  336.  
  337.  
  338. a   :   <<pred>>?  ID ...
  339.     |   ID ...
  340.     ;
  341.  
  342.  
  343. This grammar will yield a warning  when  run  through  ANTLR
  344. with  -w2  set because semantic information was not provided
  345. to indicate the validity of the second production:
  346.  
  347.  
  348. t.g, line 1: warning: alt 2 of the rule itself has no predicate to resolve ambiguity
  349.  
  350.  
  351. However, rule a will behave correctly because if pred fails,
  352. the  second production will be attempted as the default case
  353. (remember that a missing semantic predicate is equivalent to
  354. <<1>>?).  Adding a third production that began with ID would
  355. not behave correctly  as  the  last  ID-prefixed  production
  356. would never be matched.
  357.  
  358.      As  a  more complicated example, consider the following
  359. incorrectly disambiguated grammar:
  360.  
  361.  
  362. a   :   b
  363.     |   <<pred1>>? ID
  364.     |   <<pred2>>? NUM
  365.     ;
  366.  
  367. b   :   <<pred3>>? ID
  368.     |   NUM
  369.     ;
  370.  
  371.  
  372. Rule a cannot predict which production to match upon  looka-
  373. head  ID  or  NUM.   Alternatives  2  and 3 have been disam-
  374. biguated, but the first production hoists a  predicate  that
  375. only  "covers"  ID's.  As a result, the following message is
  376. generated by ANTLR:
  377.  
  378.  
  379. t.g, line 1: warning: alt 1 of the rule itself has no predicate to resolve ambiguity upon { NUM }
  380.  
  381.  
  382. This detection is a great help during grammar development.
  383.  
  384.      Ambiguity warnings are now  turned  off  for  decisions
  385. that  have semantic predicates covering all ambiguous looka-
  386. head sequences.
  387.  
  388. 2.3.  Failure of predicates
  389.  
  390.      Predicates that are not used in disambiguating  parsing
  391.  
  392.  
  393.  
  394.                      September 14, 1994
  395.  
  396.  
  397.  
  398.  
  399.  
  400.                             - 7 -
  401.  
  402.  
  403. decisions  are  called  validation  predicates.  Previously,
  404. validation predicates that failed during parsing printed out
  405. a message and terminated the parser:
  406.  
  407.  
  408. if ( !pred ) {fprintf(stderr, "failed predicate: 'pred'\n); exit(1);}
  409.  
  410.  
  411. The latest release of ANTLR generates a call to a macro that
  412. the user may define called zzfailed_pred(), which is  passed
  413. a string representing the predicate that failed:
  414.  
  415.  
  416. if ( !pred ) {zzfailed_pred("pred");}
  417.  
  418.  
  419. while  this  solution  is  not ideal, it is much better than
  420. before.
  421.  
  422. 3.  Infinite lookahead and Backtracking
  423.  
  424.      There are a number of grammatical constructs that  nor-
  425. mal LL(k) recursive-descent parsing cannot handle.  The most
  426. obvious example would be left-recursion, but  left-recursion
  427. can be removed by well-known algorithms.  The nastiest gram-
  428. mar construct is one in which  two  alternative  productions
  429. cannot be distinguished without examining all or most of the
  430. production.  While left-factoring can handle many  of  these
  431. cases,  some  cannot  be  handled  due to things like action
  432. placement, non-identical left-factors, or alternatives  pro-
  433. ductions that cannot be reorganized into the same rule.  The
  434. solution to the arbitrarily-large common left-factor problem
  435. is  simply  to use arbitrary lookahead; i.e., as much looka-
  436. head as necessary to uniquely determine which production  to
  437. apply.
  438.  
  439.      ANTLR 1.10 provides two mechanisms for using "infinite"
  440. amounts of lookahead.  The first is to use  semantic  predi-
  441. cates in conjunction with a user-defined function that scans
  442. arbitrarily ahead using a set of  macros  provided  in  this
  443. release.   The second is a more implicit scheme by which the
  444. user can annotate those sections of the grammar, which  defy
  445. normal  LL(k)  analysis,  with  syntactic predicates.  ANTLR
  446. will then generate code that simply tries out the  indicated
  447. alternative production to see if it would match a portion of
  448. the remaining input.  If not, the generated parser would try
  449. the  next  viable  alternative production.  This scheme is a
  450. form of selective backtracking (and,  hence,  can  recognize
  451. the  class of context free languages) where most of a parser
  452. is deterministic and only the "hard" parts  are  done  using
  453. trial-and-error.   As  a  direct  consequence, ANTLR can now
  454. generate parsers with the  semantic  flexibility  of  LL(k),
  455. that  are  stronger  than  full  LR(k)  (in theory), and are
  456. nearly  linear  in  complexity;  note  that   the   semantic
  457.  
  458.  
  459.  
  460.                      September 14, 1994
  461.  
  462.  
  463.  
  464.  
  465.  
  466.                             - 8 -
  467.  
  468.  
  469. predicates  (first  introduced in the 1.06 release) can take
  470. ANTLR-generated parsers  beyond  the  context-free  language
  471. limit into the context-sensitive.
  472.  
  473.      We  begin  this  section  by  introducing the notion of
  474. infinite lookahead through an example problem that we  solve
  475. with semantic and then with syntactic predicates.  Following
  476. this, we describe in detail the syntax and use of  syntactic
  477. predicates,  which  employ  infinite  lookahead  to  perform
  478. selective backtracking.
  479.  
  480. 3.1.  Examples
  481.  
  482.      This section presents a simple  grammar  whose  produc-
  483. tions  have common left-factors that we assume, for the sake
  484. of demonstration purposes, to be non left-factorable.   With
  485. nothing  but the grammar, ANTLR would be unable to construct
  486. a deterministic parser.  We  first  provide  a  solution  by
  487. writing  a  function  that  explicitly accesses the infinite
  488. lookahead buffer to determine  which  production  should  be
  489. attempted.   This  solution  is  efficient, but would become
  490. somewhat tedious for the programmer if it had to be done for
  491. each  such problem in a large grammar.  Fortunately, an eas-
  492. ier and more concise solution is provided by syntactic pred-
  493. icates, which we also demonstrate using the same grammar.
  494.  
  495.      Consider  ML  which  has  multiple  assignment and list
  496. statements.  E.g.,
  497.  
  498.  
  499. stat:   list Assign list ";"    <<printf("list = list\n");>>
  500.     |   list ";"                <<printf("list\n");>>
  501.     ;
  502.  
  503.  
  504. This grammar is not LL(k) for any k as list can be arbitrar-
  505. ily  long.   The following grammar using semantic predicates
  506. to access the infinite lookahead buffer to  explicitly  com-
  507. pute which production will be matched.
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.                      September 14, 1994
  527.  
  528.  
  529.  
  530.  
  531.  
  532.                             - 9 -
  533.  
  534.  
  535.  
  536. /* example use of the infinite lookahead buffer macros
  537.  * compile with:
  538.  *              antlr list.g
  539.  *              dlg parser.dlg scan.c
  540.  *              cc -Iantlr_includes -DZZINF_LOOK -o list list.c scan.c err.c
  541.  */
  542. #header <<#include "charbuf.h">>
  543.  
  544. <<
  545. main() { ANTLR(stat(), stdin); }
  546.  
  547. /* Scan for a "=", but only before a ";" -- return 1 if found, else 0
  548.    This performs the same function as using the syntactic predicate:
  549.         (list Assign list ";")?
  550.    but uses a semantic predicate coupled with the infinite-lookahead feature.
  551.    It is somewhat faster as it does not actually *parse* the "list =", it just
  552.    scans ahead.
  553.  
  554.    MUST HAVE "ZZINF_LOOK" PREPROCESSOR FLAG DEFINED
  555.    (in #header or on compiler command line)
  556.    */
  557. which()
  558. {
  559.     int i;
  560.  
  561.     for (i=1; ZZINF_LA_VALID(i); i++)
  562.     {
  563.         if ( ZZINF_LA(i) == Assign ) return 1;
  564.         else if ( ZZINF_LA(i) == Semi ) return 0;
  565.     }
  566.     return 0;
  567. }
  568. >>
  569.  
  570. #token                  "[\ \t]+"        <<zzskip();>>
  571. #token                  "\n"             <<zzskip(); zzline++;>>
  572. #token Assign   "="
  573. #token Semi             ";"
  574.  
  575. stat:  <<which()>>? list Assign list ";" <<printf("list = list\n");>>
  576.     |   list ";"                         <<printf("list\n");>>
  577.     ;
  578.  
  579. list:   "\(" elem ("," elem)* "\)"
  580.     ;
  581.  
  582. elem:   ID
  583.     |   INT
  584.     ;
  585.  
  586. #token ID               "[a-zA-Z]+"
  587. #token INT              "[0-9]+"
  588.  
  589.  
  590.  
  591.  
  592.                      September 14, 1994
  593.  
  594.  
  595.  
  596.  
  597.  
  598.                            - 10 -
  599.  
  600.  
  601. The  infinite lookahead buffer may be accessed with the fol-
  602. lowing macros:
  603.  
  604. ZZINF_LA(i)
  605.      Return the ith token of lookahead relative to the  cur-
  606.      rent  position.   Hence,  ZZINF_LA(1)..ZZINF_LA(k)  are
  607.      equivalent to LA(1)..LA(k).  The difference is  that  i
  608.      can range from the current token of lookahead until the
  609.      last token of lookahead with ZZINF_LA(i).
  610.  
  611. ZZINF_LATEXT(i)
  612.      Identical to ZZINF_LA(i) except that the  text  of  the
  613.      ith token is returned.
  614.  
  615. ZZINF_LA_VALID(i)
  616.      Returns 1 if i if at least i non-EOF tokens are left in
  617.      the input stream else it returns 0.
  618.  
  619. Naturally,  the  use  of  infinite  lookahead  by   defining
  620. ZZINF_LOOK  is  inconsistent with interactive parsers as the
  621. entire input stream is read in before parsing begins.
  622.  
  623.      As mentioned above, this method could  be  tedious  for
  624. large  grammars,  hence, ANTLR provides a more elegant solu-
  625. tion.  The same problem can be solved with a syntactic pred-
  626. icate by changing rule stat in the following way:
  627.  
  628.  
  629. stat:   ( list Assign list ";" )?       <<printf("list = list\n");>>
  630.     |   list ";"                        <<printf("list\n");>>
  631.     ;
  632.  
  633.  
  634. Using this implicit method, the need for the semantic predi-
  635. cate and the which() function disappears.
  636.  
  637.      Let's now consider a small chunk of the vast C++ decla-
  638. ration  syntax.   Can you tell exactly what type of object f
  639. is after having seen the left parenthesis?
  640.  
  641.  
  642. int f(
  643.  
  644.  
  645. The answer is "no.".  Object f could be an integer  initial-
  646. ized to some previously defined symbol a:
  647.  
  648.  
  649. int f(a);
  650.  
  651.  
  652. or a function prototype or definition:
  653.  
  654.  
  655.  
  656.  
  657.  
  658.                      September 14, 1994
  659.  
  660.  
  661.  
  662.  
  663.  
  664.                            - 11 -
  665.  
  666.  
  667.  
  668. int f(float a) {...}
  669.  
  670.  
  671. The  following is a greatly simplified grammar for these two
  672. declaration types:
  673.  
  674.  
  675. decl:   type ID "\(" expr_list "\)" ";"
  676.         |       type ID "\(" arg_decl_list "\)" func_def
  677.         ;
  678.  
  679.  
  680. One notices that left-factoring type ID "\(" would be  triv-
  681. ial  because  our  grammar is so small and the left-prefixes
  682. are identical.  However, if  a  user  action  were  required
  683. before  recognition  of  the  reference  to rule type, left-
  684. factoring would not be possible:
  685.  
  686.  
  687. decl:   <</* dummy init action so next action is not taken as init */>>
  688.                 <<printf("var init\n");>>  type ID "\(" expr_list "\)" ";"
  689.         |       <<printf("func def\n");>>  type ID "\(" arg_decl_list "\)" func_def
  690.         ;
  691.  
  692.  
  693. The solution to the  problem  involves  looking  arbitrarily
  694. ahead  (type could be arbitrarily big, in general) to deter-
  695. mine what appears after the left-parenthesis.  This  problem
  696. is  easily solved implicitly by using the new (...)? syntac-
  697. tic predicate:
  698.  
  699.  
  700. decl:   (   <<;>> <<printf("var init\n");>> type ID "\(" expr_list "\)" ";"   )?
  701.         |       <<printf("func def\n");>>  type ID "\(" arg_decl_list "\)" func_def
  702.         ;
  703.  
  704.  
  705. The (...)? says that it is impossible to  decide,  from  the
  706. left  edge  of  rule decl with a finite amount of lookahead,
  707. which production to predict.  Any grammar construct inside a
  708. (...)?  block is attempted and, if it fails, the next alter-
  709. native production that could match the input  is  attempted.
  710. This  represents  selective  backtracking  and is similar to
  711. allowing ANTLR parsers to guess  without  being  "penalized"
  712. for being wrong.  Note that the first action of any block is
  713. considered an init action and, hence, cannot be disabled (by
  714. placing  it inside {...}) since it may define variables; the
  715. first action of the block is a dummy action.
  716.  
  717.      At this point, some readers  may  argue  that  scanning
  718. ahead  arbitrarily  far,  using the infinite lookahead via a
  719. semantic or syntactic predicate,  renders  the  parser  non-
  720. linear  in  nature.   While  this  is  true, the slowdown is
  721.  
  722.  
  723.  
  724.                      September 14, 1994
  725.  
  726.  
  727.  
  728.  
  729.  
  730.                            - 12 -
  731.  
  732.  
  733. negligible as the parser is mostly linear.  Further,  it  is
  734. better  to  have  a  capability that is slightly inefficient
  735. than not to have the capability at all.
  736.  
  737. 3.2.  Syntactic Predicates
  738.  
  739.      Just as semantic predicates indicate when a  production
  740. is  valid, syntactic predicates also indicate when a produc-
  741. tion is a candidate for recognition.  The difference lies in
  742. the  type of information used to predict alternative produc-
  743. tions.  Semantic predicates  employ  information  about  the
  744. "meaning"  of  the  input  (e.g.,  symbol table information)
  745. whereas syntactic predicates employ  structural  information
  746. like  normal  LL(k) parsing decisions.  Syntactic predicates
  747. specify a grammatical construct that must  be  seen  on  the
  748. input  stream  for a production to be valid.  Moreover, this
  749. construct may match input streams that are arbitrarily long;
  750. normal LL(k) parsers are restricted to using the next k sym-
  751. bols of lookahead.  This section describes the form and  use
  752. of syntactic predicates as well as their implementation.
  753.  
  754. 3.2.1.  Syntactic Predicate Form
  755.  
  756.      Syntactic predictions have the form
  757.  
  758.  
  759. (  )?
  760.  
  761.  
  762. or, the shorthand form
  763.  
  764.  
  765. (  )?
  766.  
  767.  
  768. which is identical to
  769.  
  770.  
  771. (  )?
  772.  
  773.  
  774. where   and  are arbitrary Extended BNF (EBNF) grammar frag-
  775. ments that do not define new nonterminals.  The notation  is
  776. similar to the ()* and ()+ closure blocks already present in
  777. PCCTS.  The meaning of the long form syntactic predicate is:
  778. "If  is matched on the input stream, attempt to recognize ."
  779. Note the similarity to the semantic predicate:
  780.  
  781.  
  782. <<>>?
  783.  
  784.  
  785. which means: "If  evaluates  to  true  at  parser  run-time,
  786. attempt to match ."
  787.  
  788.  
  789.  
  790.                      September 14, 1994
  791.  
  792.  
  793.  
  794.  
  795.  
  796.                            - 13 -
  797.  
  798.  
  799.      Decisions,  which  are  nondeterministic (non-LL(k) for
  800. finite k), are resolved via (..)? in the following manner:
  801.  
  802.  
  803. a   :   1
  804.     |   2
  805.    ...
  806.     |   ( i )? i
  807.    ...
  808.     |   j
  809.    ...
  810.     |   n
  811.     ;
  812.  
  813.  
  814. where productions i and j  are  mutually  nondistinguishable
  815. from  the  left-edge.   If  production i fails, production j
  816. will be attempted.  Typically, the number of syntactic pred-
  817. icates  employed  is  n-1  where n is the number of mutually
  818. nondeterministic productions in a decision; the last produc-
  819. tion is attempted by default.
  820.  
  821.      When  a  production  to  be predicted must be predicted
  822. with itself (nothing less sophisticated  is  sufficient)  or
  823. when  efficiency  is  not a major concern, the short form is
  824. used:
  825.  
  826.  
  827. a   :   1
  828.     |   2
  829.    ...
  830.     |   ( i )?
  831.    ...
  832.     |   j
  833.    ...
  834.     |   n
  835.     ;
  836.  
  837.  
  838. 3.2.2.  Modified LL(k) Parsing Scheme
  839.  
  840.      Decisions that are not augmented with syntactic  predi-
  841. cates  are parsed deterministically with finite lookahead up
  842. to depth k as is normal for PCCTS-generated  parsers.   When
  843. at  least  one syntactic predicate is present in a decision,
  844. rule recognition proceeds as follows:
  845.  
  846.  
  847. [1]  Find the first viable production; i.e. the  first  pro-
  848.      duction  in  the alternative list predicted by the cur-
  849.      rent finite  lookahead,  according  to  the  associated
  850.      finite-lookahead prediction-expression.
  851.  
  852. [2]  If  the  first  element  in  that  production  is not a
  853.  
  854.  
  855.  
  856.                      September 14, 1994
  857.  
  858.  
  859.  
  860.  
  861.  
  862.                            - 14 -
  863.  
  864.  
  865.      syntactic predicate, predict that production and go  to
  866.      [4]  else attempt to match its predicting grammar frag-
  867.      ment.
  868.  
  869. [3]  If the grammar fragment is matched, predict the associ-
  870.      ated production and go to [4] else find the next viable
  871.      production and go to [2].
  872.  
  873. [4]  Proceed with the normal recognition of  the  production
  874.      predicted in [2] or [3].
  875.  
  876.  
  877. For successful predicates, both the predicting grammar frag-
  878. ment and  the  remainder  of  the  production  are  actually
  879. matched,  hence,  the  short  form,  ()?,  actually matches
  880. twice -- once to predict and once to apply  normally.
  881.  
  882. 3.2.3.  Syntactic Predicate Placement
  883.  
  884.      Syntactic predicates may only appear as the first  ele-
  885. ment  of  a  production because that is the only place deci-
  886. sions are required.  For example, the  (..)?  block  in  the
  887. first production of following grammar has little utility.
  888.  
  889.  
  890. a   :   1 (  )?
  891.     |   2
  892.    ...
  893.     |   n
  894.     ;
  895.  
  896.  
  897. There is no question that  is to be matched after 1 and try-
  898. ing to predict this situation is redundant.
  899.  
  900.      Syntactic predicates may appear on the left edge of any
  901. production  within  any  subrule, not just in productions at
  902. the rule block level.
  903.  
  904. 3.2.4.  Nested Syntactic Predicate Invocation
  905.  
  906.      Because syntactic predicates may reference any  defined
  907. nonterminal and because of the recursive nature of grammars,
  908. it is possible for the parser to return to a  point  in  the
  909. grammar  which  had  already  requested  backtracking.  This
  910. nested invocation poses no problem from a theoretical  point
  911. of  view,  but  can cause unexpected parsing delays in prac-
  912. tice.
  913.  
  914. 3.2.5.  Grammar Fragments within Syntactic Predicates
  915.  
  916.      The grammar fragments within ()? may be any valid PCCTS
  917. production  right-hand-side;  i.e. any expression except new
  918. nonterminal definitions.   may contain semantic actions  and
  919.  
  920.  
  921.  
  922.                      September 14, 1994
  923.  
  924.  
  925.  
  926.  
  927.  
  928.                            - 15 -
  929.  
  930.  
  931. semantic  predicates,  although only the semantic predicates
  932. will be executed during prediction.
  933.  
  934. 3.2.6.  Efficiency
  935.  
  936.      In terms of efficiency, the order of  alternative  pro-
  937. ductions  in  a  decision  is significant.  Productions in a
  938. PCCTS grammar are always attempted in the  order  specified.
  939. For  example,  the  parsing strategy outline above indicates
  940. that the following rule is most efficient  when  1  is  less
  941. complex than 2.
  942.  
  943.  
  944. a       :   (1)?
  945.     |   2
  946.         ;
  947.  
  948.  
  949.      Any  parsing  decisions  made  inside a (..)? block are
  950. made deterministically unless they themselves  are  prefixed
  951. with syntactic predicates.  For example,
  952.  
  953.  
  954. a       :       ( (A)+ X | (B)+ X )?
  955.         |       (A)* Y
  956.         ;
  957.  
  958.  
  959. specifies  that  the parser should attempt to match the non-
  960. predicated subrule
  961.  
  962.  
  963.         (       (A)+ X
  964.         |       (B)+ X
  965.         )
  966.  
  967.  
  968. using normal the normal finite-lookahead  parsing  strategy.
  969. If a sentence recognizable by this grammar fragment is found
  970. on the input stream, then restore the state of the parser to
  971. what  it  was  before the predicate invocation and parse the
  972. grammar fragment again; else, if the attempt  failed,  apply
  973. the next production in the outer block:
  974.  
  975.  
  976. (A)* Y
  977.  
  978.  
  979. 3.2.7.  Resolving Ambiguous C++ Statements
  980.  
  981.      Quoting  from  Ellis and Stroustrup ["The Annotated C++
  982. Reference Manual," Margaret A. Ellis and Bjarne  Stroustrup,
  983. Addison  Wesley  Publishing Company; Reading, Massachusetts;
  984. 1990],
  985.  
  986.  
  987.  
  988.                      September 14, 1994
  989.  
  990.  
  991.  
  992.  
  993.  
  994.                            - 16 -
  995.  
  996.  
  997.      "There is an ambiguity in  the  grammar  involving
  998.      expression-statements and declarations... The gen-
  999.      eral cases cannot be resolved  without  backtrack-
  1000.      ing... In particular, the lookahead needed to dis-
  1001.      ambiguate this case is not limited."
  1002.  
  1003. The authors use the following examples to make their point:
  1004.  
  1005.  
  1006. T(*a)->m=7;     // expression-statement
  1007. T(*a)(int);     // declaration
  1008.  
  1009.  
  1010. Clearly, the two types of statements are not distinguishable
  1011. from  the left as an arbitrary amount of symbols may be seen
  1012. before a decision can be made; here, the ->  symbol  is  the
  1013. first  clue  that the first example is a statement.  Quoting
  1014. Ellis and Stroustrup further,
  1015.  
  1016.      "In a parser with backtracking the  disambiguating
  1017.      rule can be stated very simply:
  1018.      [1] If it looks like a declaration, it is; otherwise
  1019.      [2] if it looks like an expression, it is; otherwise
  1020.      [3] it is a syntax error."
  1021.  
  1022. The solution in PCCTS using syntactic predicates is simply:
  1023.  
  1024.  
  1025. stat:   (declaration)?
  1026.     |   expression
  1027.     ;
  1028.  
  1029.  
  1030. The  semantics  of  rule stat are exactly that of the quoted
  1031. solution.  The production declaration will, however, be rec-
  1032. ognized  twice  upon  a  valid  declaration and once upon an
  1033. expression to decide that it is not a declaration.
  1034.  
  1035. 3.2.8.  Revisiting the ML Example
  1036.  
  1037.      To illustrate the utility of the full form of syntactic
  1038. predicates,  reconsider  the grammar for the ML-style state-
  1039. ments provided in the example section above:
  1040.  
  1041.  
  1042. stat:   list "=" list ";"
  1043.         |       list ";"
  1044.         ;
  1045.  
  1046.  
  1047. Rule stat is not LL because list could be  arbitrarily  long
  1048. and,  hence, predicting which production to apply beforehand
  1049. is impossible with a finite lookahead depth.  There are  two
  1050. solutions  in using syntactic predicates, one more efficient
  1051.  
  1052.  
  1053.  
  1054.                      September 14, 1994
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.                            - 17 -
  1061.  
  1062.  
  1063. than the other.  The first method is, as before, to specify:
  1064.  
  1065.  
  1066. stat:   (list "=" list ";")?
  1067.         |       list ";"
  1068.         ;
  1069.  
  1070.  
  1071. However,  this  specification unnecessarily matches the list
  1072. following the assignment operator twice.  A more  efficient,
  1073. but functionally equivalent, specification is as follows:
  1074.  
  1075.  
  1076. stat:   (list "=")? list "=" list ";"
  1077.         |       list ";"
  1078.         ;
  1079.  
  1080.  
  1081. This description indicates that, as soon as the "=" has been
  1082. seen, the first production is uniquely predicted.
  1083.  
  1084. 3.2.9.  Syntactic Predicates Effect on Grammar Analysis
  1085.  
  1086.      ANTLR still constructs normal LL(k) decisions  through-
  1087. out  predicated  parsers.  Only when necessary are arbitrary
  1088. lookahead predictors used.  Constructing LL(k) parsers is an
  1089. exponential  problem  that  ANTLR  goes  to great lengths to
  1090. avoid or reduce in  size  on  average.   Unfortunately,  for
  1091. large  grammars  and  k values of more than 2 or 3 ANTLR can
  1092. take an impractical amount of time.  Part of the benefit  of
  1093. (..)?  blocks is that, by definition, they defy LL(k) analy-
  1094. sis.  Hence, the exponential, full LL(k) grammar analysis is
  1095. turned  off  for  any  production beginning with a syntactic
  1096. predicate.  In its place, a linear  approximation  to  LL(k)
  1097. analysis,  called  LL1(k), is used.  This reduces the number
  1098. of times that arbitrary lookahead (..)? blocks are attempted
  1099. unnecessarily,  though no finite lookahead decision is actu-
  1100. ally required as  the  arbitrary  lookahead  mechanism  will
  1101. accurately predict the production.
  1102.  
  1103.      If  the current finite lookahead can predict which pro-
  1104. duction to apply, syntactic predicates  are  not  evaluated.
  1105. For example, referring to the C++ declaration versus expres-
  1106. sion grammar example above, if the current input token  were
  1107. 42,   rule   stat   would  immediately  attempt  the  second
  1108. production -- expression.  On the other hand, if the current
  1109. input  token  were  ID,  then  the declaration rule would be
  1110. attempted before attempting expression.  If neither  produc-
  1111. tions successfully match the input, a syntax occurs.
  1112.  
  1113.      When  constructing  finite  lookahead sets, the grammar
  1114. fragment within the (..)? block is ignored.  In other words,
  1115. FIRSTk(()?) is FIRSTk().
  1116.  
  1117.  
  1118.  
  1119.  
  1120.                      September 14, 1994
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.                            - 18 -
  1127.  
  1128.  
  1129. 3.2.10.   The  Effect of Nondeterminism upon Translation and
  1130. Semantic Predicates
  1131.  
  1132.      Syntactic predicates are, by definition, not guaranteed
  1133. to  match  the current input.  Therefore, actions with side-
  1134. effects, for which no "undo" exists, cannot be executed dur-
  1135. ing  nondeterministic  syntactic  prediction ("guess" mode).
  1136. This section describes how ANTLR handles  the  execution  of
  1137. user-supplied actions and semantic predicates.
  1138.  
  1139. 3.2.10.1.  The Effect upon User Actions
  1140.  
  1141.      PCCTS  language  specifications do not allow the execu-
  1142. tion of any semantic action during a syntactic prediction as
  1143. no  undo  mechanism  exists; this conservative scheme avoids
  1144. affecting the parser state in an irreversible  manner.   The
  1145. only  exception to this rule is that initialization actions,
  1146. which  usually  define  variables  visible  to  the   entire
  1147. rule/function,  are  not  enclosed  in if {..} statements to
  1148. "gate" them out; hence,  initialization  actions  with  side
  1149. effects must be avoided by the PCCTS user.
  1150.  
  1151. 3.2.10.2.  The Effect upon Semantic Predicates
  1152.  
  1153.      Semantic  predicates  are always evaluated because they
  1154. are  restricted  to  side-effect-free  expressions.   During
  1155. arbitrary lookahead prediction, the semantic predicates that
  1156. are evaluated must be  functions  of  values  computed  when
  1157. actions  were turned on.  For example, if your grammar has a
  1158. predicate that examines the symbol table, all symbols needed
  1159. to  direct  the parse during prediction must be entered into
  1160. the table before prediction has begun.  Consider the follow-
  1161. ing  grammar fragment which recognizes simplified C declara-
  1162. tions.
  1163.  
  1164.  
  1165. decl:   "typedef" type declarator ";"                                           /* define new type */
  1166.         |       ( type declarator "\{" )? type declarator func_body     /* define function */
  1167.         |       type declarators ";"                                            /* def/decl var(s) */
  1168.         ;
  1169.  
  1170. type:   built_in_type
  1171.         |       <<is_type(LATEXT(1))>>? ID
  1172.         ;
  1173.  
  1174. declarator
  1175.         :       ...
  1176.                 /* recognizes a declarator such as "array[3]" */
  1177.                 /* add symbols, both types and vars, to the symbol table */
  1178.         ;
  1179.  
  1180.  
  1181. This rule is unnecessarily inefficient, but will  illustrate
  1182. the     evaluation    of    semantic    predicates    during
  1183.  
  1184.  
  1185.  
  1186.                      September 14, 1994
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.                            - 19 -
  1193.  
  1194.  
  1195. nondeterministic prediction.  For the purposes of  our  dis-
  1196. cussion,  we  restrict  new  types  to be introduced using a
  1197. typedef (structures and unions are not  allowed).   Consider
  1198. the recognition of the two sentences:
  1199.  
  1200.  
  1201. typedef int My_int;
  1202. My_int i;
  1203.  
  1204.  
  1205. The  first production of rule decl will match the first sen-
  1206. tence, adding My_int to the symbol table  as  a  type  name.
  1207. Production two of decl attempts to match the second sentence
  1208. with its syntactic predicate.  Rule type is  entered,  which
  1209. evaluates  is_type(LATEXT(1)) (where is_type() is some user-
  1210. defined function that looks up its symbol  argument  in  the
  1211. symbol  table and returns true if that symbol is defined and
  1212. is a type).  Because the text of the current token of looka-
  1213. head,  My_int,  is  a valid type, the predicate evaluates to
  1214. true.  Production two of type is applicable semantically and
  1215. is,  therefore, applied.  After consuming My_int, the parser
  1216. successfully applies declarator to i.  The next input  token
  1217. is ; which does not match .  The nondeterministic prediction
  1218. fails and production three is predicted by  default  and  is
  1219. applied.
  1220.  
  1221.      The second production of rule decl could not be rewrit-
  1222. ten as
  1223.  
  1224.  
  1225. ( type declarator func_body     )?      /* define function */
  1226.  
  1227.  
  1228. because, presumably, a func_body  could  define  new  types.
  1229. The  actions  that  add  these new types to the symbol table
  1230. would not be executed, however, as the parser  would  be  in
  1231. nondeterministic  mode.   Although  the  semantic predicates
  1232. would be evaluated correctly, the  symbol  table  would  not
  1233. hold  the  information  necessary to parse the function body
  1234. during nondeterministic prediction.  Also, this revision  is
  1235. very  inefficient  as  it  would  match the entire function,
  1236. which could be large, twice.
  1237.  
  1238. 3.2.11.  Comparing the Use of Semantic and Syntactic  Predi-
  1239. cates
  1240.  
  1241.      Language  constructs  exists that are totally ambiguous
  1242. syntactically, but easily distinguishable semantically.  For
  1243. example,  array references and function calls in Fortran are
  1244. identical syntactically, but  very  different  semantically.
  1245. The  associated  grammatical  description is non-LL(k), non-
  1246. LALR(k), and  non-context-free;  not  even  backtracking  or
  1247. infinite lookahead will help this problem.
  1248.  
  1249.  
  1250.  
  1251.  
  1252.                      September 14, 1994
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.                            - 20 -
  1259.  
  1260.  
  1261.  
  1262. expratom:   ID "\(" expr_list "\)"
  1263.         |   ID "\(" expr_list "\)"
  1264.        ...
  1265.         ;
  1266.  
  1267.  
  1268. where  expr_list  is  some  rule  matching a comma-separated
  1269. expression list.  Putting (..)? around the first alternative
  1270. production  will  not  change the fact that both productions
  1271. match the same sentence.  However, semantic  predicates  may
  1272. be used to semantically disambiguate the rule:
  1273.  
  1274.  
  1275. expratom:   <<isVar(LATEXT(1))>>?    ID "\(" expr_list "\)"
  1276.         |   <<isFunc(LATEXT(1))>>?   ID "\(" expr_list "\)"
  1277.        ...
  1278.         ;
  1279.  
  1280.  
  1281.  
  1282. 3.2.12.  Implementation
  1283.  
  1284.      The discussion thus far has described the functionality
  1285. of syntactic predicates,  but  their  implementation  is  an
  1286. equally important topic so that users can understand the new
  1287. ANTLR parsing mechanism (e.g.,  so  that  users  can  follow
  1288. along  in a debugger while tracking down bugs in their gram-
  1289. mar).
  1290.  
  1291.      Because productions are assumed to be attempted in  the
  1292. order  specified,  a nested if-then-else structure is gener-
  1293. ated.  To illustrate the integration of syntactic predicates
  1294. into  the  normal ANTLR code generation scheme, consider the
  1295. following abstract grammar.
  1296.  
  1297.  
  1298. a       :       1
  1299.    ...
  1300.         |       ( i )? i
  1301.    ...
  1302.         |       ( j )? j
  1303.    ...
  1304.         |       m
  1305.         ;
  1306.  
  1307.  
  1308. ANTLR generates the following, "slightly sanitized", code:
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.                      September 14, 1994
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.                            - 21 -
  1325.  
  1326.  
  1327.  
  1328. a()
  1329. {
  1330.     zzGUESS_BLOCK
  1331.     if ( (1, ..., k)  LOOKk(1) ) {
  1332.         match1;
  1333.     }
  1334.     else {
  1335.         zzGUESS
  1336.         if ( !zzrv && (1, ..., k)  LOOKk(i) ) {
  1337.             matchi;
  1338.             zzGUESS_DONE
  1339.             matchi;
  1340.         }
  1341.         else {
  1342.             if ( zzguessing ) zzGUESS_DONE;
  1343.             zzGUESS
  1344.             if ( !zzrv && (1, ..., k)  LOOKk(j) ) {
  1345.                 matchj;
  1346.                 zzGUESS_DONE
  1347.                 matchj;
  1348.             }
  1349.             else {
  1350.                 if ( zzguessing ) zzGUESS_DONE;
  1351.                 if ( (1, ..., k)  LOOKk(m) ) {
  1352.                     matchm;
  1353.                 }
  1354.                 else goto fail;
  1355.             }
  1356.         }
  1357.     }
  1358.     return;
  1359. fail:
  1360.     if ( zzguessing ) {zzGUESS_FAIL;}
  1361.     gen syntax error message;
  1362.     resynch parser;
  1363. }
  1364.  
  1365.  
  1366. where LOOKk() is the set of lookahead k-tuples that  predict
  1367. .  This notation is used as a convenience here whereas ANTLR
  1368. generates decisions that use as little lookahead as possible
  1369. in practice.
  1370.  
  1371.      The macros/variables themselves are defined as follows:
  1372.  
  1373. zzGUESS_BLOCK
  1374.      Define a block of memory to  hold  the  current  parser
  1375.      state and the return value of setjmp(), zzrv.
  1376.  
  1377. zzGUESS
  1378.      Save  the  current  parser state, turn on guessing mode
  1379.      and call setjmp() to  record  the  current  C  run-time
  1380.      stack  state.   The  result  of setjmp() is placed into
  1381.  
  1382.  
  1383.  
  1384.                      September 14, 1994
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.                            - 22 -
  1391.  
  1392.  
  1393.      zzrv.
  1394.  
  1395. zzGUESS_FAIL
  1396.      Long jump -- restore the C run-time stack to the  state
  1397.      it held before guessing began.
  1398.  
  1399. zzGUESS_DONE
  1400.      Restore  the  parser state to the previously saved con-
  1401.      tents.
  1402.  
  1403. zzguessing
  1404.      This variable is 1 if a prediction is currently  under-
  1405.      way  and  0  when  normal  parsing is proceeding.  User
  1406.      actions are turned off when this variable is 1.
  1407.  
  1408. zzrv
  1409.      This variable is the result of doing the setjmp() call,
  1410.      which returns 0 always.  When a longjmp() occurs, the C
  1411.      run-time stack will be reset to the state held  at  the
  1412.      time  of the setjmp() and zzrv will be set to a nonzero
  1413.      value.  In the view of the C program, it will appear as
  1414.      if  the  setjmp()  has  returned  without  ever  having
  1415.      attempted the code in the if  following  it;  execution
  1416.      continues past the if the second time.
  1417.  
  1418. All  semantic  actions  except  initialization  actions  are
  1419. enclosed in
  1420.  
  1421.  
  1422. zzNON_GUESS_MODE {
  1423.         user-defined-semantic-action;
  1424. }
  1425.  
  1426.  
  1427. so that they  can  be  "turned  off"  during  a  prediction.
  1428. zzNON_GUESS_MODE is defined as follows:
  1429.  
  1430.  
  1431. if ( !zzguessing )
  1432.  
  1433.  
  1434. The  effect  of this type of code generation is that a stack
  1435. of parser states is maintained such that nested nondetermin-
  1436. istic predictions can be made.
  1437.  
  1438.      As  an  optimization, when the prediction grammar frag-
  1439. ment  for  a  production  is  regular,  simpler  recognition
  1440. schemes could be used.
  1441.  
  1442. 4.  DLG Enhancements
  1443.  
  1444.      There have been a number of changes to dlg from 1.06 to
  1445. 1.10.  The main difference is that DLG execution speed is up
  1446. to  7  times  faster  than  the 1.06 version.  A -Wambiguity
  1447.  
  1448.  
  1449.  
  1450.                      September 14, 1994
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.                            - 23 -
  1457.  
  1458.  
  1459. option has been added to indicate where ambiguities  in  DLG
  1460. specifications  exists.   It  numbers  the  expressions  and
  1461. prints out for an accept state the possible expressions that
  1462. could be recognized.  Also, a macro called ANTLRs() has been
  1463. added that behaves just like ANTLR() except that it  accepts
  1464. input from a string rather than a stream:
  1465.  
  1466.  
  1467. #define ANTLRs(rule, string)    {...}
  1468.  
  1469.  
  1470. 5.  Linear-Approximation Lookahead
  1471.  
  1472.      ANTLR-generated  parsers predict which rule alternative
  1473. to match by examining up to k symbols of lookahead.   Unfor-
  1474. tunately,  computing  (during  ANTLR  grammar  analysis) and
  1475. examining (during parser execution) the set of  possible  k-
  1476. sequences  is  an  exponentially  large  problem.   A linear
  1477. approximation to this full lookahead  exists  that  requires
  1478. linear time to compute and to test; further, this approxima-
  1479. tion handles the majority of  parsing  lookahead  decisions.
  1480. To  avoid  the,  possibly  exponential,  computation of full
  1481. lookahead, ANTLR attempts to use  the  linear  approximation
  1482. first --  computing  full  lookahead  as a last resort.  The
  1483. reason that ANTLR occasionally goes "off the deep end"  when
  1484. analyzing  some  big  grammars is that ANTLR found a parsing
  1485. decision that could  not  be  solved  with  the  approximate
  1486. lookahead  and required exponential time to compute the full
  1487. lookahead.
  1488.  
  1489.      Because the approximation has  linear  time  and  space
  1490. complexity, its lookahead depth can be much deeper than that
  1491. of the full lookahead.  Consequently, the approximate looka-
  1492. head  is  sometimes stronger than the full lookahead because
  1493. it can look farther ahead without consuming  an  impractical
  1494. amount  of system resources.  We have added an ANTLR option,
  1495. called -ck n, that allows the user to specify how  deep  the
  1496. linear approximation analysis should go before giving up and
  1497. trying the full lookahead computation.  This new feature  is
  1498. best described with an example:
  1499.  
  1500.  
  1501. a       :       (A B|C D) E
  1502.         |       A D F
  1503.         ;
  1504.  
  1505.  
  1506. The  full LL(2) lookahead (as would be computed by "antlr -k
  1507. 2 ...") is summarized in the following table
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515.  
  1516.                      September 14, 1994
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.                            - 24 -
  1523.  
  1524.  
  1525.                  +------------------------+
  1526.                  |         LL(2)          |
  1527.                  +----------+-------------+
  1528.                  |Lookahead | Alternative |
  1529.                  +----------+-------------+
  1530.                  |   A B    |      1      |
  1531.                  |   C D    |      1      |
  1532.                  |   A D    |      2      |
  1533.                  +----------+-------------+
  1534.  
  1535. whereas the linear approximate lookahead, denoted LL1(2) (as
  1536. would be computed by "antlr -ck 2 ..."), is
  1537.  
  1538.                 +--------------------------+
  1539.                 |         LL1(2)           |
  1540.                 +------------+-------------+
  1541.                 | Lookahead  | Alternative |
  1542.                 +------------+-------------+
  1543.                 |{A,C} {B,D} |      1      |
  1544.                 |  {A} {D}   |      2      |
  1545.                 +------------+-------------+
  1546.  
  1547. where lookahead {A,C} {B,D} implies that the first symbol of
  1548. lookahead can be either A or C and the second can be  either
  1549. B  or  D;  this  lookahead  therefore  matches  the  set  of
  1550. sequences {A B, A D, C B, C D},  which  is  like  the  cross
  1551. product  of the sets (note the loss of sequence information,
  1552. which results in the approximation).  The decision is LL(2),
  1553. but  is  not  LL1(2)  because the sequence A D predicts both
  1554. alternatives (i.e., A can be seen first by both and D can be
  1555. seen second).  However, if ANTLR were allowed to look 3 sym-
  1556. bols ahead -- LL1(3) -- the linear  approximation  would  be
  1557. sufficient and the complex LL(3) would not be computed.  The
  1558. LL1(3) information ("antlr -ck 3") is summarized in the fol-
  1559. lowing table:
  1560.  
  1561.  
  1562.               +------------------------------+
  1563.               |           LL1(3)             |
  1564.               +----------------+-------------+
  1565.               |   Lookahead    | Alternative |
  1566.               +----------------+-------------+
  1567.               |{A,C} {B,D} {E} |      1      |
  1568.               |  {A} {D} {F}   |      2      |
  1569.               +----------------+-------------+
  1570.  
  1571. Notice  that,  now,  the third symbol of lookahead alone can
  1572. uniquely identify which alternative to predict.
  1573.  
  1574.      Let's augment our example to have  one  LL(2)  decision
  1575. and one LL1(3) decision:
  1576.  
  1577.  
  1578.  
  1579.  
  1580.  
  1581.  
  1582.                      September 14, 1994
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.                            - 25 -
  1589.  
  1590.  
  1591.  
  1592. a   :   (A B | C D) E   /* LL1(3) */
  1593.     |   A D F b
  1594.     ;
  1595. b   :   (A B | C D) Z   /* LL(2) */
  1596.     |   A D Z
  1597.     ;
  1598.  
  1599.  
  1600. Although  LL(3)  ("antlr  -k 3 ...") handles both the LL1(3)
  1601. and LL(2) decisions, we can make  ANTLR  and  the  resulting
  1602. parser  more efficient by specifying "antlr -ck 3 -k 2 ...".
  1603. The resulting parser decisions are illustrated in  the  fol-
  1604. lowing (sanitized) code fragment:
  1605.  
  1606.  
  1607. a()
  1608. {
  1609.     /* there are no sequence comparisons for rule 'a' because LL1(3)
  1610.      * is sufficient and full LL(3) analysis is not invoked
  1611.      */
  1612.     if ( LA(1){A,C} && LA(2){B,D} && LA(3)==E ) {
  1613.         if ( (LA(1)==A) ) {
  1614.             zzmatch(A); zzCONSUME;
  1615.             zzmatch(B); zzCONSUME;
  1616.         }
  1617.         else if ( (LA(1)==C) ) {
  1618.             zzmatch(C); zzCONSUME;
  1619.             zzmatch(D); zzCONSUME;
  1620.         }
  1621.         zzmatch(E); zzCONSUME;
  1622.     }
  1623.     else if ( (LA(1)==A) && (LA(2)==D) && (LA(3)==F) ) {
  1624.         zzmatch(A); zzCONSUME;
  1625.         zzmatch(D); zzCONSUME;
  1626.         zzmatch(F); zzCONSUME;
  1627.         b();
  1628.     }
  1629. }
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.  
  1646.  
  1647.  
  1648.                      September 14, 1994
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.                            - 26 -
  1655.  
  1656.  
  1657.  
  1658. b()
  1659. {
  1660.         /* LL(2) decision */
  1661.     if ( (LA(1)==A&&LA(2)==B) || (LA(1)==C&&LA(2)==D) ) {
  1662.         if ( (LA(1)==A) ) {
  1663.             zzmatch(A); zzCONSUME;
  1664.             zzmatch(B); zzCONSUME;
  1665.         }
  1666.         else if ( (LA(1)==C) ) {
  1667.             zzmatch(C); zzCONSUME;
  1668.             zzmatch(D); zzCONSUME;
  1669.         }
  1670.     }
  1671.     else if ( LA(1)==A&&LA(2)==D ) {
  1672.         zzmatch(A); zzCONSUME;
  1673.         zzmatch(D); zzCONSUME;
  1674.         zzmatch(Z); zzCONSUME;
  1675.     }
  1676. }
  1677.  
  1678.  
  1679.      These  examples  are  small and, hence, the savings are
  1680. not apparent, but the  "compressed"  approximation  to  full
  1681. lookahead can be used to reduce the ANTLR execution time and
  1682. resulting parser speed/size.
  1683.  
  1684. 6.  Faster Compilation of ANTLR-Generated Parsers
  1685.  
  1686.      Previous versions of  ANTLR  used  macros  rather  than
  1687. function  calls for many operations during parsing.  Because
  1688. the macros were invoked numerous times, compilation of these
  1689. files was slow and generated large object files.  The opera-
  1690. tions are now, by default, function calls which makes compi-
  1691. lation  about  2  times  as fast and results in object files
  1692. about half as large.  The macros can be used if necessary by
  1693. defining  ZZUSE_MACROS on the compile line (-DZZUSE_MACROS).
  1694.  
  1695. 7.  Linking Together Multiple ANTLR Parsers
  1696.  
  1697.      Because of the lack of sophisticated "information  hid-
  1698. ing"  in  C, many ANTLR program symbols are globally visible
  1699. and,  hence,  linking   multiple   ANTLR-generated   parsers
  1700. together  would  cause  many  name  collisions.  To overcome
  1701. this, we have introduced a new ANTLR directive:
  1702.  
  1703.  
  1704. #parser "my_parser_name"
  1705.  
  1706.  
  1707. which prefixes all global, externally visible  symbols  with
  1708. prefix  my_parser_name_  (remember this when debugging ANTLR
  1709. parsers).  This, clearly,  renders  the  previous  "generate
  1710. prefix"  option  (-gp)  obsolete.   Variables, functions and
  1711.  
  1712.  
  1713.  
  1714.                      September 14, 1994
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.                            - 27 -
  1721.  
  1722.  
  1723. rule names are remapped through  the  inclusion  of  a  file
  1724. called  remap.h,  which  is automatically generated by ANTLR
  1725. when it encounters a #parser directive.  In the  future,  we
  1726. expect  this  to  be  the name of a C++ object of some class
  1727. Parser;  variables  and  functions  will  be  referenced  as
  1728. my_parser_name.var_or_func.
  1729.  
  1730.      Consider  the  following  ANTLR example.  Files t.g and
  1731. t2.g are identical except for the parser name.
  1732.  
  1733. File t.g
  1734.  
  1735.  
  1736.   #header <<#include "charbuf.h">>
  1737.  
  1738.   #parser "t"
  1739.  
  1740.   <<
  1741.   void parse_t()
  1742.   {
  1743.       ANTLR(a(), stdin);
  1744.   }
  1745.   >>
  1746.  
  1747.   #token "[\ \t\n]"       <<zzskip();>>
  1748.  
  1749.   a : INT INT
  1750.     ;
  1751.  
  1752.   #token INT "[0-9]+"
  1753.  
  1754.  
  1755. File t2.g
  1756.  
  1757.  
  1758.   #header <<#include "charbuf.h">>
  1759.  
  1760.   #parser "t2"
  1761.  
  1762.   <<
  1763.   void parse_t2()
  1764.   {
  1765.       ANTLR(a(), stdin);
  1766.   }
  1767.   >>
  1768.  
  1769.   #token "[\ \t\n]"       <<zzskip();>>
  1770.  
  1771.   a : INT INT
  1772.     ;
  1773.  
  1774.   #token INT "[0-9]+"
  1775.  
  1776.  
  1777.  
  1778.  
  1779.  
  1780.                      September 14, 1994
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786.                            - 28 -
  1787.  
  1788.  
  1789. File main.c
  1790.  
  1791.  
  1792.   #include <stdio.h>
  1793.  
  1794.   extern void parse_ter();
  1795.   extern void parse_ter2();
  1796.  
  1797.   main()
  1798.   {
  1799.       parse_ter();
  1800.       parse_ter2();
  1801.   }
  1802.  
  1803.  
  1804. File makefile
  1805.  
  1806.  
  1807.  
  1808.  
  1809.  
  1810.  
  1811.  
  1812.  
  1813.  
  1814.  
  1815.  
  1816.  
  1817.  
  1818.  
  1819.  
  1820.  
  1821.  
  1822.  
  1823.  
  1824.  
  1825.  
  1826.  
  1827.  
  1828.  
  1829.  
  1830.  
  1831.  
  1832.  
  1833.  
  1834.  
  1835.  
  1836.  
  1837.  
  1838.  
  1839.  
  1840.  
  1841.  
  1842.  
  1843.  
  1844.  
  1845.  
  1846.                      September 14, 1994
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852.                            - 29 -
  1853.  
  1854.  
  1855.  
  1856.   DLG_FILE = parser.dlg
  1857.   ERR_FILE = err.c
  1858.   HDR_FILE = stdpccts.h
  1859.   TOK_FILE = tokens.h
  1860.   K = 1
  1861.   ANTLR_H = ../h
  1862.   BIN = ../bin
  1863.   ANTLR = ../bin/antlr
  1864.   DLG = $(BIN)/dlg
  1865.   CFLAGS = -I. -I$(ANTLR_H) -g
  1866.   AFLAGS = -fe err.c -fl parser.dlg -ft tokens.h -fr remap.h -fm mode.h  \
  1867.            -gt -gk
  1868.   AFLAGS2= -fe t2_err.c -fl t2_parser.dlg -ft t2_tokens.h -fr t2_remap.h \
  1869.            -fm t2_mode.h -gt -gk
  1870.   DFLAGS = -C2 -i
  1871.   GRM = t.g
  1872.   SRC1 = scan.c t.c err.c
  1873.   SRC2 = t2.c t2_scan.c t2_err.c main.c
  1874.   OBJ1 = scan.o t.o err.o
  1875.   OBJ2 = t2.o t2_scan.o t2_err.o main.o
  1876.   CC=g++
  1877.  
  1878.   t: $(OBJ1) $(OBJ2)
  1879.       $(CC) -o t $(CFLAGS) $(OBJ1) $(OBJ2)
  1880.  
  1881.   t.o : mode.h tokens.h t.g
  1882.  
  1883.   scan.c mode.h : parser.dlg
  1884.       $(DLG) $(DFLAGS) parser.dlg scan.c
  1885.  
  1886.   t.c parser.dlg tokens.h : t.g
  1887.       $(ANTLR) $(AFLAGS) t.g
  1888.  
  1889.   t2.o : t2_mode.h t2_tokens.h t2.g
  1890.  
  1891.   t2_scan.c t2_mode.h : t2_parser.dlg
  1892.       $(DLG) $(DFLAGS) -m t2_mode.h t2_parser.dlg t2_scan.c
  1893.  
  1894.   t2.c t2_parser.dlg t2_tokens.h : t2.g
  1895.       $(ANTLR) $(AFLAGS2) t2.g
  1896.  
  1897.  
  1898. The input to the parser is 4 integers because  each  of  the
  1899. invoked parsers matches 2.
  1900.  
  1901.      The preprocessor symbol zzparser is set the parser name
  1902. string specified in the #parser "name" directive.
  1903.  
  1904.      WARNING: the remapping of symbols to  avoid  collisions
  1905. is  not a foolproof system.  For example, if you have a rule
  1906. named type and a field in a structure named type, the  field
  1907. name  will  get renamed as well -- this is the price you pay
  1908. for being able to link things together without C++.
  1909.  
  1910.  
  1911.  
  1912.                      September 14, 1994
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.                            - 30 -
  1919.  
  1920.  
  1921. 8.  Creating Customized Syntax Error Routines
  1922.  
  1923.      Many users have asked how to create their  own  zzsyn()
  1924. error handling routine.  Here's how:
  1925.  
  1926. [1]  Make new zzsyn() with same parameters.
  1927.  
  1928. [2]  Define  the  preprocessor symbol USER_ZZSYN on the com-
  1929.      pile line (-DUSER_ZZSYN).
  1930.  
  1931. 9.  Lexical Changes to ANTLR Input
  1932.  
  1933.      The manner in which ANTLR interprets user  actions  has
  1934. changed.   Strings,  character  literals, and C/C++ comments
  1935. are now totally ignored.  For example,
  1936.  
  1937.  
  1938. <<
  1939.         // nothing in here is examined $1 ' "
  1940.         /* or in here ' " $ #[jfd] '"''''" */
  1941.         '"' // that's a character
  1942.         "'" // that's an apostrophe
  1943.         "$1 is", $1 /* $1 inside string is ignored */
  1944. >>
  1945.  
  1946.  
  1947. As a result of this change, you may experience a slight dif-
  1948. ference  in  how ANTLR treats your actions.  Comments inside
  1949. actions are still passed through to the parser.
  1950.  
  1951.      C++ comments are now accepted  outside  of  actions  as
  1952. well:
  1953.  
  1954.  
  1955. // this rule does nothing
  1956. a : ;
  1957.  
  1958.  
  1959. Watch out for this:
  1960.  
  1961.  
  1962.   ...
  1963.   << // a comment >>
  1964.   ...
  1965. a : A
  1966.   ;
  1967.  
  1968.  
  1969. The  C++ style comment in side the action will scarf til end
  1970. of line and ignore the >> end action symbol.  This could  be
  1971. avoided, but I'm feeling lazy just now.
  1972.  
  1973.  
  1974.  
  1975.  
  1976.  
  1977.  
  1978.                      September 14, 1994
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984.                            - 31 -
  1985.  
  1986.  
  1987. 10.  New ANTLR Options
  1988.  
  1989.      Release  1.10  introduces  the following ANTLR command-
  1990. line options:
  1991.  
  1992. -    ANTLR now accepts input  from  stdin  by  using  the  -
  1993.      option; e.g.,
  1994.  
  1995.  
  1996.      antlr -
  1997.  
  1998.  
  1999.      A  file called stdin.c is created as the output parser.
  2000.  
  2001. -ck nUse up to n symbols of lookahead when using  compressed
  2002.      (linear  approximation) lookahead.  This type of looka-
  2003.      head is very cheap to compute and is  attempted  before
  2004.      full  LL(k) lookahead, which is of exponential complex-
  2005.      ity in the worst  case.   In  general,  the  compressed
  2006.      lookahead  can  be  much  deeper (e.g, -ck 10) than the
  2007.      full lookahead (which usually must be less than 4).
  2008.  
  2009. -fm mode_file
  2010.      Rename file with lexical mode definitions,  mode.h,  to
  2011.      file.
  2012.  
  2013. -fr file
  2014.      Rename  file  which  remaps  globally  visible symbols,
  2015.      remap.h, to file.  This  file  is  only  created  if  a
  2016.      #parser directive is found.
  2017.  
  2018. -prc on
  2019.      Turn  on the computation and hoisting of predicate con-
  2020.      text.
  2021.  
  2022. -prc off
  2023.      Turn off the computation and hoisting of predicate con-
  2024.      text.   This  option  makes  1.10  behave like the 1.06
  2025.      release with option -pr on (default).
  2026.  
  2027. -w1  Set low warning level.  Do not warn if semantic  predi-
  2028.      cates and/or (...)? blocks are assumed to cover ambigu-
  2029.      ous alternatives.
  2030.  
  2031. -w2  Ambiguous parsing  decisions  yield  warnings  even  if
  2032.      semantic predicates or (...)? blocks are used.  Warn if
  2033.      -prc on and some lookahead  sequences  are  not  disam-
  2034.      biguated with a hoisted predicate.
  2035.  
  2036. 11.  ANTLR Generates "Androgynous" Code
  2037.  
  2038.      The  distribution  source  of  1.06 PCCTS was generated
  2039. using 1.06 on a 32-bit machine.  Unfortunately,  the  source
  2040. code  dumped  bit  sets to arrays of unsigned's according to
  2041.  
  2042.  
  2043.  
  2044.                      September 14, 1994
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050.                            - 32 -
  2051.  
  2052.  
  2053. the word size of the machine that  generated  the  parser --
  2054. regardless  of the word size of the various target machines.
  2055. To overcome this, ANTLR always dumps its bit sets as  arrays
  2056. of  unsigned char, which are 8 bits (or more) on any machine
  2057. that we'd ever want to work on.  As a result,  ANTLR  itself
  2058. should  bootstrap  on any machine with a C compiler a enough
  2059. memory.  We have gotten it to compile with 16-bit  Microsoft
  2060. and Borland C on the PC with only a few whimpers.  The make-
  2061. files in the ANTLR and DLG  directories  have  sections  for
  2062. each of the various compilers.
  2063.  
  2064. 12.  Printing out grammars
  2065.  
  2066.      Using the -p option generates grammar listings that are
  2067. somewhat nicer.
  2068.  
  2069. 13.  C Grammar Changes
  2070.  
  2071.      The C grammar example has been augmented with  a  -both
  2072. option  that  prints  out both K&R and ANSI C prototypes for
  2073. functions defined in the input file.  E.g.
  2074.  
  2075.  
  2076. % proto -both
  2077. void f(a,b)
  2078. int a;
  2079. char *b;
  2080. {;}
  2081. ^D
  2082.  void
  2083. #ifdef __STDC__
  2084. f( int a, char *b )
  2085. #else
  2086. f( a, b )
  2087.  int a;
  2088.  char *b;
  2089. #endif
  2090.  
  2091.  
  2092. Functions that already employ ANSI C style argument  defini-
  2093. tions are handled as well.
  2094.  
  2095. 14.  C++ Now Compiles ANTLR Itself
  2096.  
  2097.      We  have  modified  the source code of ANTLR to compile
  2098. under C++.  It is not written to  take  advantage  of  C++'s
  2099. extensions  to  C, however, except in rare instances.  C++'s
  2100. stricter type checking motivated the modification.
  2101.  
  2102. 15.  New Preprocessor Symbol
  2103.  
  2104.      ANTLR now generates a #define called ANTLR_VERSION that
  2105. is  set  to  the version of ANTLR that generated the parser.
  2106. For this release, you will see:
  2107.  
  2108.  
  2109.  
  2110.                      September 14, 1994
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116.                            - 33 -
  2117.  
  2118.  
  2119.  
  2120. #define ANTLR_VERSION 110
  2121.  
  2122.  
  2123. in the output files, which is an integer equivalent  of  the
  2124. version number.
  2125.  
  2126. 16.  Attribute Warning
  2127.  
  2128.      A  number  of users have had trouble with the charptr.h
  2129. attributes.  Please note that they do not  make  copies  and
  2130. that  the  memory is freed after the scope exits.  For exam-
  2131. ple, this is wrong because the memory for the  $1  attribute
  2132. of  A  or  B in the (...) scope will be freed upon exit even
  2133. though $0 will still point to it.
  2134.  
  2135.  
  2136. #header<<#include "charptr.h">>
  2137.  
  2138. <<
  2139. #include "charptr.c"
  2140. main() { ANTLR(a(),stdin); }
  2141. >>
  2142.  
  2143. #token "[\ \t\n]"       << zzskip(); >>
  2144.  
  2145. a: "ick" ("A" << $0=$1; >>| "B" << $0=$1; >>) "ugh"
  2146.    << printf("$1, $2, $3 are %s, %s, %s\n",$1, $2, $3); >>
  2147.  ;
  2148.  
  2149.  
  2150. One should make a copy of the local attribute (or use  char-
  2151. buf.h)  as  the  mem  is  freed  at  the  end  of  the scope
  2152. ($0=strdup($1);).
  2153.  
  2154. 17.  Generation of Line Information
  2155.  
  2156.      The normal form of line information is:
  2157.  
  2158.  
  2159. # line_number "file"
  2160.  
  2161.  
  2162. However, many compilers, such as Borland C, prefer it as
  2163.  
  2164.  
  2165. #line line_number "file"
  2166.  
  2167.  
  2168. This can be easily changed by looking file generic.h in  the
  2169. antlr directory for the following:
  2170.  
  2171.  
  2172.  
  2173.  
  2174.  
  2175.  
  2176.                      September 14, 1994
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182.                            - 34 -
  2183.  
  2184.  
  2185.  
  2186. /* User may redefine how line information looks */
  2187. #define LineInfoFormatStr "# %d \"%s\"\n"
  2188.  
  2189.  
  2190. Simply change it as your compiler wants it and recompile the
  2191. antlr source.
  2192.  
  2193. 18.  Incompatibilities
  2194.  
  2195.      There should be very few  incompatibilities  with  your
  2196. 1.06-based  grammars.   Should  you  find  any please let us
  2197. know.
  2198.  
  2199.      1.06 semantic predicates were not hoisted into  parsing
  2200. decisions without the -pr flag (now obsolete).  In 1.10, the
  2201. use of a predicate indicates that it may be hoisted.
  2202.  
  2203.      Semantic predicates used to halt  parser  upon  failure
  2204. whereas 1.10 does not.
  2205.  
  2206.      The  interpretation of strings, character literals, and
  2207. comments are now handled differently; see above.
  2208.  
  2209. 19.  Future Directions
  2210.  
  2211.      This section  briefly  describes  some  of  the  future
  2212. enhancements  either being discussed, planned, or developed.
  2213.  
  2214. o    A graphical user interface is planned for  ANTLR  gram-
  2215.      mars    that   will   allow   the   simultaneous   dis-
  2216.      play/manipulation of BNF and syntax diagram representa-
  2217.      tions of user grammars.
  2218.  
  2219. o    A source-to-source translator-generator called SORCERER
  2220.      is in prototype form.  It's input looks like ANTLR  and
  2221.      is integrated so that one description will contain lex-
  2222.      ical, syntactic, and tree-translation information.
  2223.  
  2224. o    A number of  groups  are  working  on  a  C++  grammar.
  2225.      Things  are  starting  to  heat up as it is pretty much
  2226.      certain that 1.10 ANTLR is the minimum necessary system
  2227.      to parse C++.
  2228.  
  2229. o    A  code-generator  generator, called PIGG, is in proto-
  2230.      type form.
  2231.  
  2232. o    An assembler generator is in prototype form.
  2233.  
  2234. o    DLG backtracking will be added.
  2235.  
  2236. o    A new "magic" token type, ".", will be introduced which
  2237.      means "match any single token."
  2238.  
  2239.  
  2240.  
  2241.  
  2242.                      September 14, 1994
  2243.  
  2244.  
  2245.  
  2246.  
  2247.  
  2248.                            - 35 -
  2249.  
  2250.  
  2251. o    A  new  operator  will  be  introduced, "~", which will
  2252.      allow constructs like  ~(A|B|C) --  implying  "match  a
  2253.      single token not from the set {A, B, C}."
  2254.  
  2255. o    A new ANTLR directive will be introduced:
  2256.  
  2257.  
  2258.      #tokclass name { token_list }
  2259.  
  2260.  
  2261.      that  creates a set of tokens like the #errclass direc-
  2262.      tive, but one which can be referenced in  the  grammar.
  2263.      For example:
  2264.  
  2265.  
  2266.      #tokclass AOP { "-" "+" }
  2267.      #tokclass MOP { "/" " }
  2268.      #tokclass OP  { AOP MOP }
  2269.       ...
  2270.      e : e1 ( AOP e1 )* ;
  2271.      e1: e2 ( MOP e2 )* ;
  2272.       ...
  2273.  
  2274.  
  2275. o    Simple  left-factoring  will  be  introduced  to remove
  2276.      identical left  factors  from  alternative  productions
  2277.      (assuming user actions do not interfere).
  2278.  
  2279. o    A  version  of ANTLR (called ANTLR-lite?) is being con-
  2280.      sidered that would accept most ANTLR  description  syn-
  2281.      tax, delay grammar analysis to run time (where it could
  2282.      be done much more quickly -- with  nonexponential  com-
  2283.      plexity),   scan   for  tokens  with  an  NFA  regular-
  2284.      expression interpreter rather a DFA, and place all out-
  2285.      put  in  one nice little file.  The reduction in parser
  2286.      size would be substantial, but  at  a  parser  run-time
  2287.      cost.
  2288.  
  2289. 20.  Portability
  2290.  
  2291.      PCCTS  1.10 is known to compile "out of the box" on the
  2292. following machines and/or operating systems:
  2293.  
  2294. [1]  DECSTATION 5000
  2295.  
  2296. [2]  SGI, Running IRIX 4.0.5
  2297.  
  2298. [3]  Sun SparcStation (cc, gcc, g++, Cfront)
  2299.  
  2300. [4]  DOS and OS/2, Microsft C 6.0, 16 bit
  2301.  
  2302. [5]  DOS and OS/2, Borland C/C++, 16 bit
  2303.  
  2304. [6]  OS/2, IBM C-Set/2, 32 bit
  2305.  
  2306.  
  2307.  
  2308.                      September 14, 1994
  2309.  
  2310.  
  2311.  
  2312.  
  2313.  
  2314.                            - 36 -
  2315.  
  2316.  
  2317. [7]  VAX C under VMS
  2318.  
  2319. [8]  Linux 0.99, gcc/g++
  2320.  
  2321. [9]  NeXT box
  2322.  
  2323. [10] Amiga, AmigaDOS--SAS/C Development System V 5.10b
  2324.  
  2325. 21.  Beta Testers
  2326.  
  2327.      The following is a group of persons (listed  alphabeti-
  2328. cally) that, in some way, have helped shape and/or debug the
  2329. latest release of PCCTS.
  2330.  
  2331. [1]  Steven Anderson, (sea@ahpcrc.umn.edu)
  2332.  
  2333. [2]  Douglas        B.        Cuthbertson,        (cuthbert-
  2334.      sond@gw1.hanscom.af.mil)
  2335.  
  2336. [3]  Peter Dahl, (dahl@mckinley.ee.umn.edu)
  2337.  
  2338. [4]  Ed Harfmann, (mdbs!ed@dynamo.ecn.purdue.edu)
  2339.  
  2340. [5]  Randy Helzerman, (helz@ecn.purdue.edu)
  2341.  
  2342. [6]  Stephen Hite, (shite@sinkhole.unf.edu)
  2343.  
  2344. [7]  Dana "Muck" Hoggatt, (mdbs!muck@dynamo.ecn.purdue.edu)
  2345.  
  2346. [8]  Roy Levow, (roy@gemini.cse.fau.edu)
  2347.  
  2348. [9]  John Mejia, (mejia@mckinley.ee.umn.edu)
  2349.  
  2350. [10] David Poole, (dpoole@nitrogen.oscs.montana.edu)
  2351.  
  2352. [11] Russell Quong, (quong@ecn.purdue.edu)
  2353.  
  2354. [12] Aaron Sawdey, (sawdey@mckinley.ee.umn.edu)
  2355.  
  2356. [13] Fred                Scholldorf,                (scholl-
  2357.      dorf@nuclear.physics.sunysb.edu)
  2358.  
  2359. [14] Sumana Srinivasan, (Sumana_Srinivasan@next.com)
  2360.  
  2361. [15] Ariel Tamches, (tamches@cs.wisc.edu)
  2362.  
  2363.  
  2364.  
  2365.  
  2366.  
  2367.  
  2368.  
  2369.  
  2370.  
  2371.  
  2372.  
  2373.  
  2374.                      September 14, 1994
  2375.  
  2376.  
  2377.